package com.travel.admin.api.fallback;


import com.travel.admin.api.client.RotationChartApi;
import com.travel.admin.constants.ServiceConstant;
import com.travel.admin.domain.RotationChart;
import com.travel.admin.enums.LogLevelEnum;
import com.travel.admin.exception.TravelExceptionCode;
import com.travel.admin.util.LogUtil;
import com.travel.admin.util.prewarn.PreWarnUtil;
import com.travel.admin.web.response.HandleResp;
import feign.hystrix.FallbackFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author hezhenlin
 * @Date 2021/12/4 5:33 下午
 * @Version 1.0
 */
@Component
public class RotationChartApiFallback implements FallbackFactory<RotationChartApi> {

    @Autowired
    private PreWarnUtil preWarnUtil;

    @Override
    public RotationChartApi create(Throwable throwable) {
        return new RotationChartApi() {


            @Override
            public HandleResp<RotationChart> findById(Long id) {
                String key = "服务降级 findById";
                LogUtil.addLog(LogLevelEnum.ERROR, key);
                this.handleException(key);
                return HandleResp.failedResponse(TravelExceptionCode.SYSTEM_ERROR,null);
            }

            @Override
            public HandleResp<Integer> addRotationChart(RotationChart rotationChart) {
                String key = "服务降级 addRotationChart";
                LogUtil.addLog(LogLevelEnum.ERROR, key);
                this.handleException(key);
                return HandleResp.failedResponse(TravelExceptionCode.SYSTEM_ERROR,null);
            }

            @Override
            public HandleResp<Integer> updateRotationChart(Long id, RotationChart rotationChart) {
                String key = "服务降级 updateRotationChart";
                LogUtil.addLog(LogLevelEnum.ERROR, key);
                this.handleException(key);
                return HandleResp.failedResponse(TravelExceptionCode.SYSTEM_ERROR,null);
            }

            @Override
            public HandleResp<Integer> deleteRotationChart(Long id) {
                String key = "服务降级 deleteRotationChart";
                LogUtil.addLog(LogLevelEnum.ERROR, key);
                this.handleException(key);
                return HandleResp.failedResponse(TravelExceptionCode.SYSTEM_ERROR,null);
            }

            @Override
            public HandleResp<Integer> deleteRotationCharts(List<Long> ids) {
                String key = "服务降级 deleteRotationCharts";
                LogUtil.addLog(LogLevelEnum.ERROR, key);
                this.handleException(key);
                return HandleResp.failedResponse(TravelExceptionCode.SYSTEM_ERROR,null);
            }


            /**
             * 统一处理异常，对于调用其他模块返回过来的RuntimeException则直接抛出
             */
            private void handleException(String method) {
                // 预警
                preWarnUtil.warn(ServiceConstant.TRAVEL_SERVICE.concat(":").concat(method), TravelExceptionCode.SYSTEM_ERROR);
                if (throwable instanceof RuntimeException) {
                    LogUtil.addLog(LogLevelEnum.ERROR, "CustomFieldApiFallback,e:{}", throwable);
                    throw new RuntimeException();
                }
            }
        };
    }
}
